package com.test.lab.validate;


import org.apache.commons.lang.StringUtils;

public class Length extends Rule{

    /**
     * 默认最小长度
     */
    private static int DEFAULT_MIN_LENGTH = 0;

    /**
     * 最小长度
     */
    private int minLength;

    /**
     * 最大长度
     */
    private int maxLength;

    /**
     * 类构造方法,最小长度默认为0
     * @param maxLength 最大长度
     */
    public Length(int maxLength){
        this.minLength = DEFAULT_MIN_LENGTH;
        this.maxLength = maxLength;
    }

    /**
     * 类构造方法
     * @param minLength 最小长度
     * @param maxLength 最大长度
     */
    public Length(int minLength, int maxLength) {
        this.minLength = minLength;
        this.maxLength = maxLength;
    }

    /**
     * 判读某个字符串是否在规定长度范围内
     * @return true/false
     */
    @Override
    public boolean valid() {
        if (StringUtils.isNotBlank(this.getValue())) {
            if (this.getValue().getBytes().length < minLength
                    || this.getValue().getBytes().length > maxLength){
                this.setMessage(" length is wrong");
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }
    }
}
